package ch.bfh.medinfo.speakingsms.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class SpeakingSMSOutputReceiver extends BroadcastReceiver {
	public static final String SPEAKING_SMS_OUTPUT_BROADCAST_ACTION = "android.intent.action.SPEAKING_SMS_OUTPUT";
	public static final String ORIGIN_ADDRESS = "ORIGIN_ADDRESS";
	public static final String TEXT = "TEXT";

	public static Intent getBroadcastIntent(Context context,
			String originAddress, String bodyText) {
		// What shall be done
		Intent intent = new Intent(SPEAKING_SMS_OUTPUT_BROADCAST_ACTION);
		// Who shall do it (explicitly)
		intent.setClass(context, SpeakingSMSOutputReceiver.class);
		// Extra-Data
		intent.putExtra(ORIGIN_ADDRESS, originAddress);
		intent.putExtra(TEXT, bodyText);
		return intent;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i("ch.bfh.medinfo","SMS-Broadcast received" +intent);
		if (intent == null) {
			return;
		}
		if (intent.getAction().equals(SPEAKING_SMS_OUTPUT_BROADCAST_ACTION)) {			
			String body = intent.getExtras().getString(TEXT);
			String address=intent.getExtras().getString(ORIGIN_ADDRESS);
			Log.i("ch.bfh.medinfo","SMS received. Address: "+address+" body: "+body );
			Toast.makeText(context, ""+address+" writes: "+body, Toast.LENGTH_LONG).show();
			
		}
	}

}
